package com.erp.util;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment;

import com.erp.Interface.OnUserDataInterface;
import com.erp.Myapp;
import com.erp.R;
import com.erp.activityKView.CrmFKView;
import com.erp.bean.FileBean;
import com.erp.module.crm.xXCrm.ClientKActivity;
import com.erp.permission.PermissionListener;
import com.erp.wheelview.DateUtils;
import com.erp.wheelview.ScreenInfo;
import com.erp.wheelview.WheelMain;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.ValueCallback;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2017/1/5/005.
 * 作者：刘洋
 */
public class Dialogutil {
    /**
     * 删除图片
     *
     * @param context 上下文
     * @param listid  未删除图片的fid
     */
    public static void longitem(final Context context, final List<String> listid, final String name,final JSONObject jo, final CrmFKView FKP) {
        final AlertDialog quitdialog = new AlertDialog.Builder(context).create();
        quitdialog.show();
        quitdialog.getWindow().setContentView(R.layout.dialog_photo_long);
        TextView tv_title = (TextView) quitdialog.findViewById(R.id.tv_title);
        TextView tv_quite_confirm = (TextView) quitdialog.findViewById(R.id.tv_quite_confirm);
        TextView tv_quite_cancel = (TextView) quitdialog.findViewById(R.id.tv_quite_cancel);
        quitdialog.setCanceledOnTouchOutside(false);
        tv_title.setText("确定要删除这张图片吗？");
        final Activity activity = (Activity) context;
        tv_quite_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                quitdialog.dismiss();
            }
        });
        tv_quite_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                quitdialog.dismiss();
                final AlertDialog updatedialog = Myutil.showupdatedialog(context, "正在删除", false);
                Httputil.uplodingphoto(context, ClientKActivity.Companion.getid(), name,jo, listid, null, null, new OnUserDataInterface() {
                    @Override
                    public void onUserData(String data) {
                        Myutil.MyTost("删除图片成功");
                        if(updatedialog!=null) {
                            updatedialog.dismiss();
                        }
                        Myutil.longlogshow("clientGaugeBean","data="+data);
                        FKP.updata(data);
                    }

                    @Override
                    public void onNot(String throwable) {
                        if(updatedialog!=null) {
                            updatedialog.dismiss();
                        }
                        Myutil.MyTost("删除图片失败");
                    }
                });
            }
        });
    }
    public static long getFileSize(File file) throws Exception
    {
        long size = 0;
        if (file.exists()){
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
        } else{
            file.createNewFile();
            Log.e("获取文件大小","文件不存在!");
        }
        return size;
    }
    /**
     * 上传图片
     *
     * @param context 上下文
     * @param listid  未删除图片的fid
     * @param name    本地图片默认名字
     */
    public static void previewdialog(final Context context, final List<String> listid, final String name, final String lx, final JSONObject jo, final CrmFKView FkP) {
        final AlertDialog previewdialog = new AlertDialog.Builder(context).create();
        previewdialog.setView(new EditText(context));
        previewdialog.show();
        previewdialog.getWindow().setContentView(R.layout.dialog_preview);
        TextView tv_ok = (TextView) previewdialog.findViewById(R.id.tv_ok);
        TextView tv_no = (TextView) previewdialog.findViewById(R.id.tv_no);
        ImageView iv_preview = (ImageView) previewdialog.findViewById(R.id.iv_preview);
        final EditText et_chongmingming = (EditText) previewdialog.findViewById(R.id.et_chongmingming);
        final Activity activity = (Activity) context;
        et_chongmingming.setText(name.split("/")[name.split("/").length-1]);
        File file = new File(name);
        Long size = null;
        try {
            size = Dialogutil.getFileSize(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        final int yasuo= (int) (200/(size/1024))*100;
        x.image().bind(iv_preview, name);
        final String names = et_chongmingming.getText().toString();

        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(previewdialog!=null) {
                    previewdialog.dismiss();
                }
                final AlertDialog updatedialog = Myutil.showupdatedialog(context, "正在上传图片", false);
                Myutil.getbt(activity, name, new OnUserDataInterface() {
                    @Override
                    public void onUserData(String data) {
                        Httputil.uplodingphoto(context, ClientKActivity.Companion.getid(), lx,jo, listid, data, names, new OnUserDataInterface() {
                            @Override
                            public void onUserData(String data) {
                                if(updatedialog!=null) {
                                    updatedialog.dismiss();
                                }
                                FkP.updata(data);
                                Myutil.MyTost("上传图片成功");
                                new Handler().postDelayed(new Runnable() {
                                    @SuppressLint("StaticFieldLeak")
                                    @Override
                                    public void run() {
                                        new AsyncTask<Void, Void, Void>() {
                                            @Override
                                            protected Void doInBackground(Void... params) {
                                                File file = new File(Environment.getExternalStorageDirectory().toString() + "/erp/photo");
                                                Myutil.deleteLfile(file);
                                                return null;
                                            }
                                        }.execute();
                                    }

                                }, 3000);
                            }

                            @Override
                            public void onNot(String throwable) {
                                if(updatedialog!=null) {
                                    updatedialog.dismiss();
                                }
                                previewdialog.dismiss();
                                Myutil.MyTost("上传图片失败");
                            }
                        });
                    }

                    @Override
                    public void onNot(String throwable) {
                        Myutil.MyTost("图片压缩失败");
                    }
                });
            }
        });
        tv_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                previewdialog.dismiss();
            }
        });
    }

    /**
     * 时间选择器
     *
     * @param v       显示时间的textview
     * @param s       时间选择器的标题
     * @param context 上下文
     */
    public static void showBottoPopupWindow(final TextView v, String s,String time, final Context context) {
        final Activity activity = (Activity) context;
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = manager.getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        defaultDisplay.getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        View menuView = LayoutInflater.from(context).inflate(R.layout.show_popup_window, null);
        final PopupWindow mPopupWindow = new PopupWindow(menuView, (int) (width * 0.8),
                ActionBar.LayoutParams.WRAP_CONTENT);
        ScreenInfo screenInfoDate = new ScreenInfo((Activity) context);
        final WheelMain wheelMainDate = new WheelMain(menuView, true);
        wheelMainDate.screenheight = screenInfoDate.getHeight();
        Calendar calendar = Calendar.getInstance();
        if(time.equals("")){
            time = Myutil.getjulidate(0);
        }
        try {
            SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date=format.parse(time);
            calendar.setTime(date);
        } catch (Exception e) {
            Log.d("TAAAG","e="+e);
            e.printStackTrace();
        }
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        wheelMainDate.initDateTimePicker(year, month, day, hours, minute);
        mPopupWindow.setAnimationStyle(R.style.AnimationPreview);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f, activity);
            }
        });
        backgroundAlpha(0.6f, activity);
        TextView tv_cancle = (TextView) menuView.findViewById(R.id.tv_cancle);
        TextView tv_ensure = (TextView) menuView.findViewById(R.id.tv_ensure);
        TextView tv_pop_title = (TextView) menuView.findViewById(R.id.tv_pop_title);
        tv_pop_title.setText(s);
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                mPopupWindow.dismiss();
                backgroundAlpha(1f, activity);
            }
        });
        tv_ensure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String beginTime = wheelMainDate.getTime().toString();
                v.setText(DateUtils.formateStringH(beginTime, DateUtils.yyyyMMddHHmm));
                mPopupWindow.dismiss();
                backgroundAlpha(1f, activity);
            }
        });
    }


    /**
     * 日期选择器
     *
     * @param v       显示时间的textview
     * @param s       时间选择器的标题
     * @param context 上下文
     */
    public static void showBottoPopupWindowRiqi(final TextView v, String s,String time, final Context context) {
        final Activity activity = (Activity) context;
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = manager.getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        defaultDisplay.getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        View menuView = LayoutInflater.from(context).inflate(R.layout.show_popup_window, null);
        final PopupWindow mPopupWindow = new PopupWindow(menuView, (int) (width * 0.8),
                ActionBar.LayoutParams.WRAP_CONTENT);
        ScreenInfo screenInfoDate = new ScreenInfo((Activity) context);
        final WheelMain wheelMainDate = new WheelMain(menuView, true);
        wheelMainDate.screenheight = screenInfoDate.getHeight();
        Calendar calendar = Calendar.getInstance();
        if(time.equals("")){
            time = Myutil.getjulidate(0);
        }
        try {
            SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
            Date date=format.parse(time);
            calendar.setTime(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        wheelMainDate.initDateTimePicker(year, month, day);
        mPopupWindow.setAnimationStyle(R.style.AnimationPreview);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f, activity);
            }
        });
        backgroundAlpha(0.6f, activity);
        TextView tv_cancle = (TextView) menuView.findViewById(R.id.tv_cancle);
        TextView tv_ensure = (TextView) menuView.findViewById(R.id.tv_ensure);
        TextView tv_pop_title = (TextView) menuView.findViewById(R.id.tv_pop_title);
        tv_pop_title.setText(s);
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                mPopupWindow.dismiss();
                backgroundAlpha(1f, activity);
            }
        });
        tv_ensure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String beginTime = wheelMainDate.getTime().toString();
                v.setText(DateUtils.formateStringH(beginTime, DateUtils.yyyyMMddHHmm));
                mPopupWindow.dismiss();
                backgroundAlpha(1f, activity);
            }
        });
    }

    public static void backgroundAlpha(float bgAlpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }
    /** 下载询问对话框
     * @param s
     * @param context
     */
    public static void downloaddialog(final String s, final Context context) {
        String filename = "";
        try {
            filename = URLDecoder.decode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String[] ss=filename.split("/");
        Log.d("setDownloadListener","sssss="+ss[ss.length-1]);
        String[] sss=ss[ss.length-1].split("[?OSS]");
        for (int i = 0 ; i<sss.length;i++){
            Log.d("setDownloadListener","s="+sss[i]);
        }
        File f = new File(Environment.getExternalStorageDirectory().toString() + "/erp/download/"+sss[0]);
        if(!f.exists()){
            final AlertDialog downloaddialog = new AlertDialog.Builder(context).create();
            downloaddialog.setView(new EditText(context));
            downloaddialog.show();
            downloaddialog.getWindow().setContentView(R.layout.dialog_downloads);
            downloaddialog.setCanceledOnTouchOutside(false);
            TextView tvlujing= (TextView) downloaddialog.findViewById(R.id.tv_fujianlujing);
            final EditText etname= (EditText) downloaddialog.findViewById(R.id.et_fujianname);
            Button btok= (Button) downloaddialog.findViewById(R.id.bt_fujian_ok);
            Button btno= (Button) downloaddialog.findViewById(R.id.bt_fujian_no);
            final File folder = new File(Environment
                    .getExternalStorageDirectory().toString() + "/erp/download/");// 获得一个文件的路径
            folder.mkdirs();// 创建文件夹
            tvlujing.setText(folder.toString());
            etname.setText(sss[0]);
            btno.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    downloaddialog.dismiss();
                }
            });
            btok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showupdatedialog(s,folder.toString()+"/"+etname.getText().toString(),context);
                    downloaddialog.dismiss();
                }
            });
        }else{
            openFileReader(context,f.getAbsolutePath());
        }
    }
    public static void downloaddialog1(final FileBean bean, final Context context) {
        File f = new File(Environment.getExternalStorageDirectory().toString() + "/erp/download/"+bean.filename);
        if(!f.exists()) {
            final AlertDialog downloaddialog = new AlertDialog.Builder(context).create();
            downloaddialog.setView(new EditText(context));
            downloaddialog.show();
            downloaddialog.getWindow().setContentView(R.layout.dialog_downloads);
            downloaddialog.setCanceledOnTouchOutside(false);
            TextView tvlujing = (TextView) downloaddialog.findViewById(R.id.tv_fujianlujing);
            final EditText etname = (EditText) downloaddialog.findViewById(R.id.et_fujianname);
            Button btok = (Button) downloaddialog.findViewById(R.id.bt_fujian_ok);
            Button btno = (Button) downloaddialog.findViewById(R.id.bt_fujian_no);
            final File folder = new File(Environment
                    .getExternalStorageDirectory().toString() + "/erp/download/");// 获得一个文件的路径
            folder.mkdirs();// 创建文件夹
            tvlujing.setText("erp/download/");
            etname.setText(bean.filename);
            btno.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    downloaddialog.dismiss();
                }
            });
            btok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showupdatedialog(bean.uri, folder.toString() + "/" + etname.getText().toString(), context);
                    downloaddialog.dismiss();
                }
            });
        }else{
            openFileReader(context,f.getAbsolutePath());
        }
    }
    /**用tbs打开文件*/
    public static void openFileReader(Context context, String pathName)
    {
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("local", "true");
            JSONObject Object = new JSONObject();
            try
            {
                Object.put("pkgName",context.getApplicationContext().getPackageName());
            }
            catch (JSONException e)
            {
                e.printStackTrace();
            }
            params.put("menuData",Object.toString());
            QbSdk.getMiniQBVersion(context);
            int var6 = QbSdk.startMiniQBToLoadUrl(context, pathName, params, new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String s) {

                }
            });
            boolean a= checkBrowser("com.tencent.mtt");
            if (var6 != 0 && a) {
                openFile(new File(pathName),context);
            }else{
                int ret = QbSdk.openFileReader(context, pathName, params, new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String s) {

                    }
                });
                Log.d("bSdk","ret="+ret);
            }
    }


    private static boolean a(Context var0, String var1, String var2) {
        Intent var3 = new Intent("com.tencent.QQBrowser.action.sdk.document");
        var3.setDataAndType(Uri.fromFile(new File(var1)), "mtt/" + var2);
        List var4 = var0.getPackageManager().queryIntentActivities(var3, 0);
        boolean var5 = false;
        Iterator var6 = var4.iterator();

        while(var6.hasNext()) {
            ResolveInfo var7 = (ResolveInfo)var6.next();
            String var8 = var7.activityInfo.packageName;
            if (var8.contains("com.tencent.mtt")) {
                var5 = true;
                break;
            }
        }

        return var5;
    }

    /**
     * 下载进度对话框，并下载文件
     *
     * @param url
     */
    public static void showupdatedialog(String url, final String path, final Context context) {
        final AlertDialog updatedialog = new AlertDialog.Builder(context).create();
        updatedialog.show();
        updatedialog.setCanceledOnTouchOutside(false);
        updatedialog.getWindow().setContentView(R.layout.dialog_update);
        TextView tv_schedule = (TextView) updatedialog.findViewById(R.id.tv_schedule);
        ProgressBar pb_progressbar = (ProgressBar) updatedialog.findViewById(R.id.pb_progressbar);
        Httputil.showdownload(context, url,path, pb_progressbar, tv_schedule, new OnUserDataInterface() {
            @Override
            public void onUserData(String data) {
                updatedialog.dismiss();
                openFileReader(context,new File(path).getAbsolutePath());
            }
            @Override
            public void onNot(String throwable) {
                updatedialog.dismiss();
            }
        });
    }

    /**拍照*/
    public  static void zhiphoto(final Context context, final Fragment fragment){
        final Activity activity=(Activity) context;
        PermissionUtils.Companion.isphoto(activity, new PermissionListener() {
            @Override
            public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                Myutil.photo(activity, fragment, 1);
            }

            @Override
            public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                Myutil.MyTost("没有权限，无法拍照");
            }
        });
    }
    /**照片dialog*/
    public static void photodialog(final Context context, final Fragment fragment) {
        final Activity activity=(Activity) context;
        final AlertDialog twodialog = new AlertDialog.Builder(context).create();
        twodialog.show();
        twodialog.getWindow().setContentView(R.layout.dialog_select_photo);
        TextView tv_photograph = (TextView) twodialog.findViewById(R.id.tv_photograph);
        TextView tv_xuanphoto = (TextView) twodialog.findViewById(R.id.tv_xuan_photo);
        tv_xuanphoto.setVisibility(View.VISIBLE);
        tv_photograph.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionUtils.Companion.isphoto(activity, new PermissionListener() {
                    @Override
                    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                        Myutil.photo(activity, fragment, 1);
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                        Myutil.MyTost("没有权限，无法拍照");
                    }
                });
                twodialog.dismiss();
            }
        });
        tv_xuanphoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                if(fragment==null) {
                    activity.startActivityForResult(intent, 99);
                }else {
                    fragment.startActivityForResult(intent,99);
                }
                twodialog.dismiss();
            }
        });
    }

    /**照片dialog*/
    public static void photodialog2(final Context context, final Fragment fragment) {
        final Activity activity=(Activity) context;
        final AlertDialog twodialog = new AlertDialog.Builder(context).create();
        twodialog.show();
        twodialog.getWindow().setContentView(R.layout.dialog_select_photo);
        TextView tv_photograph = (TextView) twodialog.findViewById(R.id.tv_photograph);
        TextView tv_xuanphoto = (TextView) twodialog.findViewById(R.id.tv_xuan_photo);
        tv_xuanphoto.setVisibility(View.VISIBLE);
        tv_photograph.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionUtils.Companion.isphoto(activity, new PermissionListener() {
                    @Override
                    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                        Myutil.photo(activity, fragment, 2);
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                        Myutil.MyTost("没有权限，无法拍照");
                    }
                });
                twodialog.dismiss();
            }
        });
        tv_xuanphoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                if(fragment==null) {
                    activity.startActivityForResult(intent, 99);
                }else {
                    fragment.startActivityForResult(intent,99);
                }
                twodialog.dismiss();
            }
        });
    }

    public static void openFile(File file ,Context context) {
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setPackage("com.tencent.mtt");
            String type = getMIMEType(file);
            //设置intent的data和Type属性。android 7.0以上crash,改用provider
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Uri fileUri = FileProvider.getUriForFile(context.getApplicationContext(), "com.erp.fileProvider", file);//android 7.0以上
                intent.setDataAndType(fileUri, type);
                grantUriPermission(context, fileUri, intent);
            } else {
                intent.setDataAndType(/*uri*/Uri.fromFile(file), type);
            }
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean checkBrowser(String packageName) {

        if (packageName == null || "".equals(packageName))

            return false;

        try {

            ApplicationInfo info = Myapp.getContext().getPackageManager().getApplicationInfo(

                    packageName, PackageManager.GET_UNINSTALLED_PACKAGES);

            return true;

        } catch (PackageManager.NameNotFoundException e) {

            return false;

        }

    }

    private static void grantUriPermission(Context context, Uri fileUri, Intent intent) {
        List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            context.grantUriPermission(packageName, fileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
    }

    /**
     * 根据文件后缀名获得对应的MIME类型。
     *
     * @param file
     */
    private static String getMIMEType(File file) {

        String type = "*/*";
        String fName = file.getName();
        //获取后缀名前的分隔符"."在fName中的位置。
        int dotIndex = fName.lastIndexOf(".");
        if (dotIndex < 0) {
            return type;
        }
        /* 获取文件的后缀名 */
        String end = fName.substring(dotIndex, fName.length()).toLowerCase();
        if (end == "") return type;
        //在MIME和文件类型的匹配表中找到对应的MIME类型。
        for (int i = 0; i < MIME_MapTable.length; i++) { //MIME_MapTable??在这里你一定有疑问，这个MIME_MapTable是什么？
            if (end.equals(MIME_MapTable[i][0]))
                type = MIME_MapTable[i][1];
        }
        return type;
    }


    private static final String[][] MIME_MapTable = {
            //{后缀名， MIME类型}
            {".3gp", "video/3gpp"},
            {".apk", "application/vnd.android.package-archive"},
            {".asf", "video/x-ms-asf"},
            {".avi", "video/x-msvideo"},
            {".bin", "application/octet-stream"},
            {".bmp", "image/bmp"},
            {".c", "text/plain"},
            {".class", "application/octet-stream"},
            {".conf", "text/plain"},
            {".cpp", "text/plain"},
            {".doc", "application/msword"},
            {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
            {".xls", "application/vnd.ms-excel"},
            {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
            {".exe", "application/octet-stream"},
            {".gif", "image/gif"},
            {".gtar", "application/x-gtar"},
            {".gz", "application/x-gzip"},
            {".h", "text/plain"},
            {".htm", "text/html"},
            {".html", "text/html"},
            {".jar", "application/java-archive"},
            {".java", "text/plain"},
            {".jpeg", "image/jpeg"},
            {".jpg", "image/jpeg"},
            {".js", "application/x-javascript"},
            {".log", "text/plain"},
            {".m3u", "audio/x-mpegurl"},
            {".m4a", "audio/mp4a-latm"},
            {".m4b", "audio/mp4a-latm"},
            {".m4p", "audio/mp4a-latm"},
            {".m4u", "video/vnd.mpegurl"},
            {".m4v", "video/x-m4v"},
            {".mov", "video/quicktime"},
            {".mp2", "audio/x-mpeg"},
            {".mp3", "audio/x-mpeg"},
            {".mp4", "video/mp4"},
            {".mpc", "application/vnd.mpohun.certificate"},
            {".mpe", "video/mpeg"},
            {".mpeg", "video/mpeg"},
            {".mpg", "video/mpeg"},
            {".mpg4", "video/mp4"},
            {".mpga", "audio/mpeg"},
            {".msg", "application/vnd.ms-outlook"},
            {".ogg", "audio/ogg"},
            {".pdf", "application/pdf"},
            {".png", "image/png"},
            {".pps", "application/vnd.ms-powerpoint"},
            {".ppt", "application/vnd.ms-powerpoint"},
            {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
            {".prop", "text/plain"},
            {".rc", "text/plain"},
            {".rmvb", "audio/x-pn-realaudio"},
            {".rtf", "application/rtf"},
            {".sh", "text/plain"},
            {".tar", "application/x-tar"},
            {".txt", "text/plain"},
            {".wav", "audio/x-wav"},
            {".wma", "audio/x-ms-wma"},
            {".wmv", "audio/x-ms-wmv"},
            {".wps", "application/vnd.ms-works"},
            {".xml", "text/plain"},
            {".z", "application/x-compress"},
            {".rar","application/x-rar"},
            {".tgz","application/x-compressed-tar"},
            {".bz", "application/x-bzip"},
            {".bz2", "application/x-bzip"},
            {".zip", "application/zip'"},
            {"", "*/*"}
    };
}

//CONTENT_TYPE = {
//              '.load': 'text/html',
//        17      '.123': 'application/vnd.lotus-1-2-3',
//        18      '.3ds': 'image/x-3ds',
//        19      '.3g2': 'video/3gpp',
//        20      '.3ga': 'video/3gpp',
//        21      '.3gp': 'video/3gpp',
//        22      '.3gpp': 'video/3gpp',
//        23      '.602': 'application/x-t602',
//        24      '.669': 'audio/x-mod',
//        25      '.7z': 'application/x-7z-compressed',
//        26      '.a': 'application/x-archive',
//        27      '.aac': 'audio/mp4',
//        28      '.abw': 'application/x-abiword',
//        29      '.abw.crashed': 'application/x-abiword',
//        30      '.abw.gz': 'application/x-abiword',
//        31      '.ac3': 'audio/ac3',
//        32      '.ace': 'application/x-ace',
//        33      '.adb': 'text/x-adasrc',
//        34      '.ads': 'text/x-adasrc',
//        35      '.afm': 'application/x-font-afm',
//        36      '.ag': 'image/x-applix-graphics',
//        37      '.ai': 'application/illustrator',
//        38      '.aif': 'audio/x-aiff',
//        39      '.aifc': 'audio/x-aiff',
//        40      '.aiff': 'audio/x-aiff',
//        41      '.al': 'application/x-perl',
//        42      '.alz': 'application/x-alz',
//        43      '.amr': 'audio/amr',
//        44      '.ani': 'application/x-navi-animation',
//        45      '.anim[1-9j]': 'video/x-anim',
//        46      '.anx': 'application/annodex',
//        47      '.ape': 'audio/x-ape',
//        48      '.arj': 'application/x-arj',
//        49      '.arw': 'image/x-sony-arw',
//        50      '.as': 'application/x-applix-spreadsheet',
//        51      '.asc': 'text/plain',
//        52      '.asf': 'video/x-ms-asf',
//        53      '.asp': 'application/x-asp',
//        54      '.ass': 'text/x-ssa',
//        55      '.asx': 'audio/x-ms-asx',
//        56      '.atom': 'application/atom+xml',
//        57      '.au': 'audio/basic',
//        58      '.avi': 'video/x-msvideo',
//        59      '.aw': 'application/x-applix-word',
//        60      '.awb': 'audio/amr-wb',
//        61      '.awk': 'application/x-awk',
//        62      '.axa': 'audio/annodex',
//        63      '.axv': 'video/annodex',
//        64      '.bak': 'application/x-trash',
//        65      '.bcpio': 'application/x-bcpio',
//        66      '.bdf': 'application/x-font-bdf',
//        67      '.bib': 'text/x-bibtex',
//        68      '.bin': 'application/octet-stream',
//        69      '.blend': 'application/x-blender',
//        70      '.blender': 'application/x-blender',
//        71      '.bmp': 'image/bmp',
//        72      '.bz': 'application/x-bzip',
//        73      '.bz2': 'application/x-bzip',
//        74      '.c': 'text/x-csrc',
//        75      '.c++': 'text/x-c++src',
//        76      '.cab': 'application/vnd.ms-cab-compressed',
//        77      '.cb7': 'application/x-cb7',
//        78      '.cbr': 'application/x-cbr',
//        79      '.cbt': 'application/x-cbt',
//        80      '.cbz': 'application/x-cbz',
//        81      '.cc': 'text/x-c++src',
//        82      '.cdf': 'application/x-netcdf',
//        83      '.cdr': 'application/vnd.corel-draw',
//        84      '.cer': 'application/x-x509-ca-cert',
//        85      '.cert': 'application/x-x509-ca-cert',
//        86      '.cgm': 'image/cgm',
//        87      '.chm': 'application/x-chm',
//        88      '.chrt': 'application/x-kchart',
//        89      '.class': 'application/x-java',
//        90      '.cls': 'text/x-tex',
//        91      '.cmake': 'text/x-cmake',
//        92      '.cpio': 'application/x-cpio',
//        93      '.cpio.gz': 'application/x-cpio-compressed',
//        94      '.cpp': 'text/x-c++src',
//        95      '.cr2': 'image/x-canon-cr2',
//        96      '.crt': 'application/x-x509-ca-cert',
//        97      '.crw': 'image/x-canon-crw',
//        98      '.cs': 'text/x-csharp',
//        99      '.csh': 'application/x-csh',
//        100      '.css': 'text/css',
//        101      '.cssl': 'text/css',
//        102      '.csv': 'text/csv',
//        103      '.cue': 'application/x-cue',
//        104      '.cur': 'image/x-win-bitmap',
//        105      '.cxx': 'text/x-c++src',
//        106      '.d': 'text/x-dsrc',
//        107      '.dar': 'application/x-dar',
//        108      '.dbf': 'application/x-dbf',
//        109      '.dc': 'application/x-dc-rom',
//        110      '.dcl': 'text/x-dcl',
//        111      '.dcm': 'application/dicom',
//        112      '.dcr': 'image/x-kodak-dcr',
//        113      '.dds': 'image/x-dds',
//        114      '.deb': 'application/x-deb',
//        115      '.der': 'application/x-x509-ca-cert',
//        116      '.desktop': 'application/x-desktop',
//        117      '.dia': 'application/x-dia-diagram',
//        118      '.diff': 'text/x-patch',
//        119      '.divx': 'video/x-msvideo',
//        120      '.djv': 'image/vnd.djvu',
//        121      '.djvu': 'image/vnd.djvu',
//        122      '.dng': 'image/x-adobe-dng',
//        123      '.doc': 'application/msword',
//        124      '.docbook': 'application/docbook+xml',
//        125      '.docm': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
//        126      '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
//        127      '.dot': 'text/vnd.graphviz',
//        128      '.dsl': 'text/x-dsl',
//        129      '.dtd': 'application/xml-dtd',
//        130      '.dtx': 'text/x-tex',
//        131      '.dv': 'video/dv',
//        132      '.dvi': 'application/x-dvi',
//        133      '.dvi.bz2': 'application/x-bzdvi',
//        134      '.dvi.gz': 'application/x-gzdvi',
//        135      '.dwg': 'image/vnd.dwg',
//        136      '.dxf': 'image/vnd.dxf',
//        137      '.e': 'text/x-eiffel',
//        138      '.egon': 'application/x-egon',
//        139      '.eif': 'text/x-eiffel',
//        140      '.el': 'text/x-emacs-lisp',
//        141      '.emf': 'image/x-emf',
//        142      '.emp': 'application/vnd.emusic-emusic_package',
//        143      '.ent': 'application/xml-external-parsed-entity',
//        144      '.eps': 'image/x-eps',
//        145      '.eps.bz2': 'image/x-bzeps',
//        146      '.eps.gz': 'image/x-gzeps',
//        147      '.epsf': 'image/x-eps',
//        148      '.epsf.bz2': 'image/x-bzeps',
//        149      '.epsf.gz': 'image/x-gzeps',
//        150      '.epsi': 'image/x-eps',
//        151      '.epsi.bz2': 'image/x-bzeps',
//        152      '.epsi.gz': 'image/x-gzeps',
//        153      '.epub': 'application/epub+zip',
//        154      '.erl': 'text/x-erlang',
//        155      '.es': 'application/ecmascript',
//        156      '.etheme': 'application/x-e-theme',
//        157      '.etx': 'text/x-setext',
//        158      '.exe': 'application/x-ms-dos-executable',
//        159      '.exr': 'image/x-exr',
//        160      '.ez': 'application/andrew-inset',
//        161      '.f': 'text/x-fortran',
//        162      '.f90': 'text/x-fortran',
//        163      '.f95': 'text/x-fortran',
//        164      '.fb2': 'application/x-fictionbook+xml',
//        165      '.fig': 'image/x-xfig',
//        166      '.fits': 'image/fits',
//        167      '.fl': 'application/x-fluid',
//        168      '.flac': 'audio/x-flac',
//        169      '.flc': 'video/x-flic',
//        170      '.fli': 'video/x-flic',
//        171      '.flv': 'video/x-flv',
//        172      '.flw': 'application/x-kivio',
//        173      '.fo': 'text/x-xslfo',
//        174      '.for': 'text/x-fortran',
//        175      '.g3': 'image/fax-g3',
//        176      '.gb': 'application/x-gameboy-rom',
//        177      '.gba': 'application/x-gba-rom',
//        178      '.gcrd': 'text/directory',
//        179      '.ged': 'application/x-gedcom',
//        180      '.gedcom': 'application/x-gedcom',
//        181      '.gen': 'application/x-genesis-rom',
//        182      '.gf': 'application/x-tex-gf',
//        183      '.gg': 'application/x-sms-rom',
//        184      '.gif': 'image/gif',
//        185      '.glade': 'application/x-glade',
//        186      '.gmo': 'application/x-gettext-translation',
//        187      '.gnc': 'application/x-gnucash',
//        188      '.gnd': 'application/gnunet-directory',
//        189      '.gnucash': 'application/x-gnucash',
//        190      '.gnumeric': 'application/x-gnumeric',
//        191      '.gnuplot': 'application/x-gnuplot',
//        192      '.gp': 'application/x-gnuplot',
//        193      '.gpg': 'application/pgp-encrypted',
//        194      '.gplt': 'application/x-gnuplot',
//        195      '.gra': 'application/x-graphite',
//        196      '.gsf': 'application/x-font-type1',
//        197      '.gsm': 'audio/x-gsm',
//        198      '.gtar': 'application/x-tar',
//        199      '.gv': 'text/vnd.graphviz',
//        200      '.gvp': 'text/x-google-video-pointer',
//        201      '.gz': 'application/x-gzip',
//        202      '.h': 'text/x-chdr',
//        203      '.h++': 'text/x-c++hdr',
//        204      '.hdf': 'application/x-hdf',
//        205      '.hh': 'text/x-c++hdr',
//        206      '.hp': 'text/x-c++hdr',
//        207      '.hpgl': 'application/vnd.hp-hpgl',
//        208      '.hpp': 'text/x-c++hdr',
//        209      '.hs': 'text/x-haskell',
//        210      '.htm': 'text/html',
//        211      '.html': 'text/html',
//        212      '.hwp': 'application/x-hwp',
//        213      '.hwt': 'application/x-hwt',
//        214      '.hxx': 'text/x-c++hdr',
//        215      '.ica': 'application/x-ica',
//        216      '.icb': 'image/x-tga',
//        217      '.icns': 'image/x-icns',
//        218      '.ico': 'image/vnd.microsoft.icon',
//        219      '.ics': 'text/calendar',
//        220      '.idl': 'text/x-idl',
//        221      '.ief': 'image/ief',
//        222      '.iff': 'image/x-iff',
//        223      '.ilbm': 'image/x-ilbm',
//        224      '.ime': 'text/x-imelody',
//        225      '.imy': 'text/x-imelody',
//        226      '.ins': 'text/x-tex',
//        227      '.iptables': 'text/x-iptables',
//        228      '.iso': 'application/x-cd-image',
//        229      '.iso9660': 'application/x-cd-image',
//        230      '.it': 'audio/x-it',
//        231      '.j2k': 'image/jp2',
//        232      '.jad': 'text/vnd.sun.j2me.app-descriptor',
//        233      '.jar': 'application/x-java-archive',
//        234      '.java': 'text/x-java',
//        235      '.jng': 'image/x-jng',
//        236      '.jnlp': 'application/x-java-jnlp-file',
//        237      '.jp2': 'image/jp2',
//        238      '.jpc': 'image/jp2',
//        239      '.jpe': 'image/jpeg',
//        240      '.jpeg': 'image/jpeg',
//        241      '.jpf': 'image/jp2',
//        242      '.jpg': 'image/jpeg',
//        243      '.jpr': 'application/x-jbuilder-project',
//        244      '.jpx': 'image/jp2',
//        245      '.js': 'application/javascript',
//        246      '.json': 'application/json',
//        247      '.jsonp': 'application/jsonp',
//        248      '.k25': 'image/x-kodak-k25',
//        249      '.kar': 'audio/midi',
//        250      '.karbon': 'application/x-karbon',
//        251      '.kdc': 'image/x-kodak-kdc',
//        252      '.kdelnk': 'application/x-desktop',
//        253      '.kexi': 'application/x-kexiproject-sqlite3',
//        254      '.kexic': 'application/x-kexi-connectiondata',
//        255      '.kexis': 'application/x-kexiproject-shortcut',
//        256      '.kfo': 'application/x-kformula',
//        257      '.kil': 'application/x-killustrator',
//        258      '.kino': 'application/smil',
//        259      '.kml': 'application/vnd.google-earth.kml+xml',
//        260      '.kmz': 'application/vnd.google-earth.kmz',
//        261      '.kon': 'application/x-kontour',
//        262      '.kpm': 'application/x-kpovmodeler',
//        263      '.kpr': 'application/x-kpresenter',
//        264      '.kpt': 'application/x-kpresenter',
//        265      '.kra': 'application/x-krita',
//        266      '.ksp': 'application/x-kspread',
//        267      '.kud': 'application/x-kugar',
//        268      '.kwd': 'application/x-kword',
//        269      '.kwt': 'application/x-kword',
//        270      '.la': 'application/x-shared-library-la',
//        271      '.latex': 'text/x-tex',
//        272      '.ldif': 'text/x-ldif',
//        273      '.lha': 'application/x-lha',
//        274      '.lhs': 'text/x-literate-haskell',
//        275      '.lhz': 'application/x-lhz',
//        276      '.log': 'text/x-log',
//        277      '.ltx': 'text/x-tex',
//        278      '.lua': 'text/x-lua',
//        279      '.lwo': 'image/x-lwo',
//        280      '.lwob': 'image/x-lwo',
//        281      '.lws': 'image/x-lws',
//        282      '.ly': 'text/x-lilypond',
//        283      '.lyx': 'application/x-lyx',
//        284      '.lz': 'application/x-lzip',
//        285      '.lzh': 'application/x-lha',
//        286      '.lzma': 'application/x-lzma',
//        287      '.lzo': 'application/x-lzop',
//        288      '.m': 'text/x-matlab',
//        289      '.m15': 'audio/x-mod',
//        290      '.m2t': 'video/mpeg',
//        291      '.m3u': 'audio/x-mpegurl',
//        292      '.m3u8': 'audio/x-mpegurl',
//        293      '.m4': 'application/x-m4',
//        294      '.m4a': 'audio/mp4',
//        295      '.m4b': 'audio/x-m4b',
//        296      '.m4v': 'video/mp4',
//        297      '.mab': 'application/x-markaby',
//        298      '.man': 'application/x-troff-man',
//        299      '.mbox': 'application/mbox',
//        300      '.md': 'application/x-genesis-rom',
//        301      '.mdb': 'application/vnd.ms-access',
//        302      '.mdi': 'image/vnd.ms-modi',
//        303      '.me': 'text/x-troff-me',
//        304      '.med': 'audio/x-mod',
//        305      '.metalink': 'application/metalink+xml',
//        306      '.mgp': 'application/x-magicpoint',
//        307      '.mid': 'audio/midi',
//        308      '.midi': 'audio/midi',
//        309      '.mif': 'application/x-mif',
//        310      '.minipsf': 'audio/x-minipsf',
//        311      '.mka': 'audio/x-matroska',
//        312      '.mkv': 'video/x-matroska',
//        313      '.ml': 'text/x-ocaml',
//        314      '.mli': 'text/x-ocaml',
//        315      '.mm': 'text/x-troff-mm',
//        316      '.mmf': 'application/x-smaf',
//        317      '.mml': 'text/mathml',
//        318      '.mng': 'video/x-mng',
//        319      '.mo': 'application/x-gettext-translation',
//        320      '.mo3': 'audio/x-mo3',
//        321      '.moc': 'text/x-moc',
//        322      '.mod': 'audio/x-mod',
//        323      '.mof': 'text/x-mof',
//        324      '.moov': 'video/quicktime',
//        325      '.mov': 'video/quicktime',
//        326      '.movie': 'video/x-sgi-movie',
//        327      '.mp+': 'audio/x-musepack',
//        328      '.mp2': 'video/mpeg',
//        329      '.mp3': 'audio/mpeg',
//        330      '.mp4': 'video/mp4',
//        331      '.mpc': 'audio/x-musepack',
//        332      '.mpe': 'video/mpeg',
//        333      '.mpeg': 'video/mpeg',
//        334      '.mpg': 'video/mpeg',
//        335      '.mpga': 'audio/mpeg',
//        336      '.mpp': 'audio/x-musepack',
//        337      '.mrl': 'text/x-mrml',
//        338      '.mrml': 'text/x-mrml',
//        339      '.mrw': 'image/x-minolta-mrw',
//        340      '.ms': 'text/x-troff-ms',
//        341      '.msi': 'application/x-msi',
//        342      '.msod': 'image/x-msod',
//        343      '.msx': 'application/x-msx-rom',
//        344      '.mtm': 'audio/x-mod',
//        345      '.mup': 'text/x-mup',
//        346      '.mxf': 'application/mxf',
//        347      '.n64': 'application/x-n64-rom',
//        348      '.nb': 'application/mathematica',
//        349      '.nc': 'application/x-netcdf',
//        350      '.nds': 'application/x-nintendo-ds-rom',
//        351      '.nef': 'image/x-nikon-nef',
//        352      '.nes': 'application/x-nes-rom',
//        353      '.nfo': 'text/x-nfo',
//        354      '.not': 'text/x-mup',
//        355      '.nsc': 'application/x-netshow-channel',
//        356      '.nsv': 'video/x-nsv',
//        357      '.o': 'application/x-object',
//        358      '.obj': 'application/x-tgif',
//        359      '.ocl': 'text/x-ocl',
//        360      '.oda': 'application/oda',
//        361      '.odb': 'application/vnd.oasis.opendocument.database',
//        362      '.odc': 'application/vnd.oasis.opendocument.chart',
//        363      '.odf': 'application/vnd.oasis.opendocument.formula',
//        364      '.odg': 'application/vnd.oasis.opendocument.graphics',
//        365      '.odi': 'application/vnd.oasis.opendocument.image',
//        366      '.odm': 'application/vnd.oasis.opendocument.text-master',
//        367      '.odp': 'application/vnd.oasis.opendocument.presentation',
//        368      '.ods': 'application/vnd.oasis.opendocument.spreadsheet',
//        369      '.odt': 'application/vnd.oasis.opendocument.text',
//        370      '.oga': 'audio/ogg',
//        371      '.ogg': 'video/x-theora+ogg',
//        372      '.ogm': 'video/x-ogm+ogg',
//        373      '.ogv': 'video/ogg',
//        374      '.ogx': 'application/ogg',
//        375      '.old': 'application/x-trash',
//        376      '.oleo': 'application/x-oleo',
//        377      '.opml': 'text/x-opml+xml',
//        378      '.ora': 'image/openraster',
//        379      '.orf': 'image/x-olympus-orf',
//        380      '.otc': 'application/vnd.oasis.opendocument.chart-template',
//        381      '.otf': 'application/x-font-otf',
//        382      '.otg': 'application/vnd.oasis.opendocument.graphics-template',
//        383      '.oth': 'application/vnd.oasis.opendocument.text-web',
//        384      '.otp': 'application/vnd.oasis.opendocument.presentation-template',
//        385      '.ots': 'application/vnd.oasis.opendocument.spreadsheet-template',
//        386      '.ott': 'application/vnd.oasis.opendocument.text-template',
//        387      '.owl': 'application/rdf+xml',
//        388      '.oxt': 'application/vnd.openofficeorg.extension',
//        389      '.p': 'text/x-pascal',
//        390      '.p10': 'application/pkcs10',
//        391      '.p12': 'application/x-pkcs12',
//        392      '.p7b': 'application/x-pkcs7-certificates',
//        393      '.p7s': 'application/pkcs7-signature',
//        394      '.pack': 'application/x-java-pack200',
//        395      '.pak': 'application/x-pak',
//        396      '.par2': 'application/x-par2',
//        397      '.pas': 'text/x-pascal',
//        398      '.patch': 'text/x-patch',
//        399      '.pbm': 'image/x-portable-bitmap',
//        400      '.pcd': 'image/x-photo-cd',
//        401      '.pcf': 'application/x-cisco-vpn-settings',
//        402      '.pcf.gz': 'application/x-font-pcf',
//        403      '.pcf.z': 'application/x-font-pcf',
//        404      '.pcl': 'application/vnd.hp-pcl',
//        405      '.pcx': 'image/x-pcx',
//        406      '.pdb': 'chemical/x-pdb',
//        407      '.pdc': 'application/x-aportisdoc',
//        408      '.pdf': 'application/pdf',
//        409      '.pdf.bz2': 'application/x-bzpdf',
//        410      '.pdf.gz': 'application/x-gzpdf',
//        411      '.pef': 'image/x-pentax-pef',
//        412      '.pem': 'application/x-x509-ca-cert',
//        413      '.perl': 'application/x-perl',
//        414      '.pfa': 'application/x-font-type1',
//        415      '.pfb': 'application/x-font-type1',
//        416      '.pfx': 'application/x-pkcs12',
//        417      '.pgm': 'image/x-portable-graymap',
//        418      '.pgn': 'application/x-chess-pgn',
//        419      '.pgp': 'application/pgp-encrypted',
//        420      '.php': 'application/x-php',
//        421      '.php3': 'application/x-php',
//        422      '.php4': 'application/x-php',
//        423      '.pict': 'image/x-pict',
//        424      '.pict1': 'image/x-pict',
//        425      '.pict2': 'image/x-pict',
//        426      '.pickle': 'application/python-pickle',
//        427      '.pk': 'application/x-tex-pk',
//        428      '.pkipath': 'application/pkix-pkipath',
//        429      '.pkr': 'application/pgp-keys',
//        430      '.pl': 'application/x-perl',
//        431      '.pla': 'audio/x-iriver-pla',
//        432      '.pln': 'application/x-planperfect',
//        433      '.pls': 'audio/x-scpls',
//        434      '.pm': 'application/x-perl',
//        435      '.png': 'image/png',
//        436      '.pnm': 'image/x-portable-anymap',
//        437      '.pntg': 'image/x-macpaint',
//        438      '.po': 'text/x-gettext-translation',
//        439      '.por': 'application/x-spss-por',
//        440      '.pot': 'text/x-gettext-translation-template',
//        441      '.ppm': 'image/x-portable-pixmap',
//        442      '.pps': 'application/vnd.ms-powerpoint',
//        443      '.ppt': 'application/vnd.ms-powerpoint',
//        444      '.pptm': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
//        445      '.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
//        446      '.ppz': 'application/vnd.ms-powerpoint',
//        447      '.prc': 'application/x-palm-database',
//        448      '.ps': 'application/postscript',
//        449      '.ps.bz2': 'application/x-bzpostscript',
//        450      '.ps.gz': 'application/x-gzpostscript',
//        451      '.psd': 'image/vnd.adobe.photoshop',
//        452      '.psf': 'audio/x-psf',
//        453      '.psf.gz': 'application/x-gz-font-linux-psf',
//        454      '.psflib': 'audio/x-psflib',
//        455      '.psid': 'audio/prs.sid',
//        456      '.psw': 'application/x-pocket-word',
//        457      '.pw': 'application/x-pw',
//        458      '.py': 'text/x-python',
//        459      '.pyc': 'application/x-python-bytecode',
//        460      '.pyo': 'application/x-python-bytecode',
//        461      '.qif': 'image/x-quicktime',
//        462      '.qt': 'video/quicktime',
//        463      '.qtif': 'image/x-quicktime',
//        464      '.qtl': 'application/x-quicktime-media-link',
//        465      '.qtvr': 'video/quicktime',
//        466      '.ra': 'audio/vnd.rn-realaudio',
//        467      '.raf': 'image/x-fuji-raf',
//        468      '.ram': 'application/ram',
//        469      '.rar': 'application/x-rar',
//        470      '.ras': 'image/x-cmu-raster',
//        471      '.raw': 'image/x-panasonic-raw',
//        472      '.rax': 'audio/vnd.rn-realaudio',
//        473      '.rb': 'application/x-ruby',
//        474      '.rdf': 'application/rdf+xml',
//        475      '.rdfs': 'application/rdf+xml',
//        476      '.reg': 'text/x-ms-regedit',
//        477      '.rej': 'application/x-reject',
//        478      '.rgb': 'image/x-rgb',
//        479      '.rle': 'image/rle',
//        480      '.rm': 'application/vnd.rn-realmedia',
//        481      '.rmj': 'application/vnd.rn-realmedia',
//        482      '.rmm': 'application/vnd.rn-realmedia',
//        483      '.rms': 'application/vnd.rn-realmedia',
//        484      '.rmvb': 'application/vnd.rn-realmedia',
//        485      '.rmx': 'application/vnd.rn-realmedia',
//        486      '.roff': 'text/troff',
//        487      '.rp': 'image/vnd.rn-realpix',
//        488      '.rpm': 'application/x-rpm',
//        489      '.rss': 'application/rss+xml',
//        490      '.rt': 'text/vnd.rn-realtext',
//        491      '.rtf': 'application/rtf',
//        492      '.rtx': 'text/richtext',
//        493      '.rv': 'video/vnd.rn-realvideo',
//        494      '.rvx': 'video/vnd.rn-realvideo',
//        495      '.s3m': 'audio/x-s3m',
//        496      '.sam': 'application/x-amipro',
//        497      '.sami': 'application/x-sami',
//        498      '.sav': 'application/x-spss-sav',
//        499      '.scm': 'text/x-scheme',
//        500      '.sda': 'application/vnd.stardivision.draw',
//        501      '.sdc': 'application/vnd.stardivision.calc',
//        502      '.sdd': 'application/vnd.stardivision.impress',
//        503      '.sdp': 'application/sdp',
//        504      '.sds': 'application/vnd.stardivision.chart',
//        505      '.sdw': 'application/vnd.stardivision.writer',
//        506      '.sgf': 'application/x-go-sgf',
//        507      '.sgi': 'image/x-sgi',
//        508      '.sgl': 'application/vnd.stardivision.writer',
//        509      '.sgm': 'text/sgml',
//        510      '.sgml': 'text/sgml',
//        511      '.sh': 'application/x-shellscript',
//        512      '.shar': 'application/x-shar',
//        513      '.shn': 'application/x-shorten',
//        514      '.siag': 'application/x-siag',
//        515      '.sid': 'audio/prs.sid',
//        516      '.sik': 'application/x-trash',
//        517      '.sis': 'application/vnd.symbian.install',
//        518      '.sisx': 'x-epoc/x-sisx-app',
//        519      '.sit': 'application/x-stuffit',
//        520      '.siv': 'application/sieve',
//        521      '.sk': 'image/x-skencil',
//        522      '.sk1': 'image/x-skencil',
//        523      '.skr': 'application/pgp-keys',
//        524      '.slk': 'text/spreadsheet',
//        525      '.smaf': 'application/x-smaf',
//        526      '.smc': 'application/x-snes-rom',
//        527      '.smd': 'application/vnd.stardivision.mail',
//        528      '.smf': 'application/vnd.stardivision.math',
//        529      '.smi': 'application/x-sami',
//        530      '.smil': 'application/smil',
//        531      '.sml': 'application/smil',
//        532      '.sms': 'application/x-sms-rom',
//        533      '.snd': 'audio/basic',
//        534      '.so': 'application/x-sharedlib',
//        535      '.spc': 'application/x-pkcs7-certificates',
//        536      '.spd': 'application/x-font-speedo',
//        537      '.spec': 'text/x-rpm-spec',
//        538      '.spl': 'application/x-shockwave-flash',
//        539      '.spx': 'audio/x-speex',
//        540      '.sql': 'text/x-sql',
//        541      '.sr2': 'image/x-sony-sr2',
//        542      '.src': 'application/x-wais-source',
//        543      '.srf': 'image/x-sony-srf',
//        544      '.srt': 'application/x-subrip',
//        545      '.ssa': 'text/x-ssa',
//        546      '.stc': 'application/vnd.sun.xml.calc.template',
//        547      '.std': 'application/vnd.sun.xml.draw.template',
//        548      '.sti': 'application/vnd.sun.xml.impress.template',
//        549      '.stm': 'audio/x-stm',
//        550      '.stw': 'application/vnd.sun.xml.writer.template',
//        551      '.sty': 'text/x-tex',
//        552      '.sub': 'text/x-subviewer',
//        553      '.sun': 'image/x-sun-raster',
//        554      '.sv4cpio': 'application/x-sv4cpio',
//        555      '.sv4crc': 'application/x-sv4crc',
//        556      '.svg': 'image/svg+xml',
//        557      '.svgz': 'image/svg+xml-compressed',
//        558      '.swf': 'application/x-shockwave-flash',
//        559      '.sxc': 'application/vnd.sun.xml.calc',
//        560      '.sxd': 'application/vnd.sun.xml.draw',
//        561      '.sxg': 'application/vnd.sun.xml.writer.global',
//        562      '.sxi': 'application/vnd.sun.xml.impress',
//        563      '.sxm': 'application/vnd.sun.xml.math',
//        564      '.sxw': 'application/vnd.sun.xml.writer',
//        565      '.sylk': 'text/spreadsheet',
//        566      '.t': 'text/troff',
//        567      '.t2t': 'text/x-txt2tags',
//        568      '.tar': 'application/x-tar',
//        569      '.tar.bz': 'application/x-bzip-compressed-tar',
//        570      '.tar.bz2': 'application/x-bzip-compressed-tar',
//        571      '.tar.gz': 'application/x-compressed-tar',
//        572      '.tar.lzma': 'application/x-lzma-compressed-tar',
//        573      '.tar.lzo': 'application/x-tzo',
//        574      '.tar.xz': 'application/x-xz-compressed-tar',
//        575      '.tar.z': 'application/x-tarz',
//        576      '.tbz': 'application/x-bzip-compressed-tar',
//        577      '.tbz2': 'application/x-bzip-compressed-tar',
//        578      '.tcl': 'text/x-tcl',
//        579      '.tex': 'text/x-tex',
//        580      '.texi': 'text/x-texinfo',
//        581      '.texinfo': 'text/x-texinfo',
//        582      '.tga': 'image/x-tga',
//        583      '.tgz': 'application/x-compressed-tar',
//        584      '.theme': 'application/x-theme',
//        585      '.themepack': 'application/x-windows-themepack',
//        586      '.tif': 'image/tiff',
//        587      '.tiff': 'image/tiff',
//        588      '.tk': 'text/x-tcl',
//        589      '.tlz': 'application/x-lzma-compressed-tar',
//        590      '.tnef': 'application/vnd.ms-tnef',
//        591      '.tnf': 'application/vnd.ms-tnef',
//        592      '.toc': 'application/x-cdrdao-toc',
//        593      '.torrent': 'application/x-bittorrent',
//        594      '.tpic': 'image/x-tga',
//        595      '.tr': 'text/troff',
//        596      '.ts': 'application/x-linguist',
//        597      '.tsv': 'text/tab-separated-values',
//        598      '.tta': 'audio/x-tta',
//        599      '.ttc': 'application/x-font-ttf',
//        600      '.ttf': 'application/x-font-ttf',
//        601      '.ttx': 'application/x-font-ttx',
//        602      '.txt': 'text/plain',
//        603      '.txz': 'application/x-xz-compressed-tar',
//        604      '.tzo': 'application/x-tzo',
//        605      '.ufraw': 'application/x-ufraw',
//        606      '.ui': 'application/x-designer',
//        607      '.uil': 'text/x-uil',
//        608      '.ult': 'audio/x-mod',
//        609      '.uni': 'audio/x-mod',
//        610      '.uri': 'text/x-uri',
//        611      '.url': 'text/x-uri',
//        612      '.ustar': 'application/x-ustar',
//        613      '.vala': 'text/x-vala',
//        614      '.vapi': 'text/x-vala',
//        615      '.vcf': 'text/directory',
//        616      '.vcs': 'text/calendar',
//        617      '.vct': 'text/directory',
//        618      '.vda': 'image/x-tga',
//        619      '.vhd': 'text/x-vhdl',
//        620      '.vhdl': 'text/x-vhdl',
//        621      '.viv': 'video/vivo',
//        622      '.vivo': 'video/vivo',
//        623      '.vlc': 'audio/x-mpegurl',
//        624      '.vob': 'video/mpeg',
//        625      '.voc': 'audio/x-voc',
//        626      '.vor': 'application/vnd.stardivision.writer',
//        627      '.vst': 'image/x-tga',
//        628      '.wav': 'audio/x-wav',
//        629      '.wax': 'audio/x-ms-asx',
//        630      '.wb1': 'application/x-quattropro',
//        631      '.wb2': 'application/x-quattropro',
//        632      '.wb3': 'application/x-quattropro',
//        633      '.wbmp': 'image/vnd.wap.wbmp',
//        634      '.wcm': 'application/vnd.ms-works',
//        635      '.wdb': 'application/vnd.ms-works',
//        636      '.webm': 'video/webm',
//        637      '.wk1': 'application/vnd.lotus-1-2-3',
//        638      '.wk3': 'application/vnd.lotus-1-2-3',
//        639      '.wk4': 'application/vnd.lotus-1-2-3',
//        640      '.wks': 'application/vnd.ms-works',
//        641      '.wma': 'audio/x-ms-wma',
//        642      '.wmf': 'image/x-wmf',
//        643      '.wml': 'text/vnd.wap.wml',
//        644      '.wmls': 'text/vnd.wap.wmlscript',
//        645      '.wmv': 'video/x-ms-wmv',
//        646      '.wmx': 'audio/x-ms-asx',
//        647      '.wp': 'application/vnd.wordperfect',
//        648      '.wp4': 'application/vnd.wordperfect',
//        649      '.wp5': 'application/vnd.wordperfect',
//        650      '.wp6': 'application/vnd.wordperfect',
//        651      '.wpd': 'application/vnd.wordperfect',
//        652      '.wpg': 'application/x-wpg',
//        653      '.wpl': 'application/vnd.ms-wpl',
//        654      '.wpp': 'application/vnd.wordperfect',
//        655      '.wps': 'application/vnd.ms-works',
//        656      '.wri': 'application/x-mswrite',
//        657      '.wrl': 'model/vrml',
//        658      '.wv': 'audio/x-wavpack',
//        659      '.wvc': 'audio/x-wavpack-correction',
//        660      '.wvp': 'audio/x-wavpack',
//        661      '.wvx': 'audio/x-ms-asx',
//        662      '.x3f': 'image/x-sigma-x3f',
//        663      '.xac': 'application/x-gnucash',
//        664      '.xbel': 'application/x-xbel',
//        665      '.xbl': 'application/xml',
//        666      '.xbm': 'image/x-xbitmap',
//        667      '.xcf': 'image/x-xcf',
//        668      '.xcf.bz2': 'image/x-compressed-xcf',
//        669      '.xcf.gz': 'image/x-compressed-xcf',
//        670      '.xhtml': 'application/xhtml+xml',
//        671      '.xi': 'audio/x-xi',
//        672      '.xla': 'application/vnd.ms-excel',
//        673      '.xlc': 'application/vnd.ms-excel',
//        674      '.xld': 'application/vnd.ms-excel',
//        675      '.xlf': 'application/x-xliff',
//        676      '.xliff': 'application/x-xliff',
//        677      '.xll': 'application/vnd.ms-excel',
//        678      '.xlm': 'application/vnd.ms-excel',
//        679      '.xls': 'application/vnd.ms-excel',
//        680      '.xlsm': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
//        681      '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
//        682      '.xlt': 'application/vnd.ms-excel',
//        683      '.xlw': 'application/vnd.ms-excel',
//        684      '.xm': 'audio/x-xm',
//        685      '.xmf': 'audio/x-xmf',
//        686      '.xmi': 'text/x-xmi',
//        687      '.xml': 'application/xml',
//        688      '.xpm': 'image/x-xpixmap',
//        689      '.xps': 'application/vnd.ms-xpsdocument',
//        690      '.xsl': 'application/xml',
//        691      '.xslfo': 'text/x-xslfo',
//        692      '.xslt': 'application/xml',
//        693      '.xspf': 'application/xspf+xml',
//        694      '.xul': 'application/vnd.mozilla.xul+xml',
//        695      '.xwd': 'image/x-xwindowdump',
//        696      '.xyz': 'chemical/x-pdb',
//        697      '.xz': 'application/x-xz',
//        698      '.w2p': 'application/w2p',
//        699      '.z': 'application/x-compress',
//        700      '.zabw': 'application/x-abiword',
//        701      '.zip': 'application/zip',
//        702      '.zoo': 'application/x-zoo',
//        703      }